package com.utils;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.TourGuide.R;
import com.object.Place;

public class ListPlaceAdapter extends ArrayAdapter<Place> {

	private ArrayList<Place> places;
	private Context context;

	public ListPlaceAdapter(Context context, int textViewResourceId, ArrayList<Place> places) {
		super(context, textViewResourceId, places);
		this.context = context;
		this.places = places;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.rowplace, null);
		}
		Place p = places.get(position);
		if (p != null) {
			ImageView iv = (ImageView) v.findViewById(R.id.miniature);
			TextView tt = (TextView) v.findViewById(R.id.rowname);
			TextView bt = (TextView) v.findViewById(R.id.rowsubtitle);
			if (iv != null) {
				BitmapImage.downloadImage(iv, p.getImageUrl(), 50, 50);
			}
			if (tt != null)
				tt.setText(p.getName());
			if(bt != null)
				bt.setText(p.getCategory());
		}
		return v;
	}
}
